package cn.green.mvc.controller;

import cn.green.core.model.QFieldModel;
import cn.green.core.model.QTableModel;
import cn.green.mvc.dao.OrmTableDao;
import cn.green.mvc.entity.OrmTableEntity;
import cn.green.mvc.service.OrmTableService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.Getter;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author wang.yishu
 * @version 1.0
 * @className OrmTableController
 * @description TODO
 * @date 2025/6/16 13:47
 */
@Getter
public abstract class OrmTableController<T extends OrmTableEntity, D extends OrmTableDao<T>, Service extends OrmTableService<T, D>> {
    protected Service service;
    public OrmTableController(Service service) {
        this.service = service;
    }

    @PostMapping("/e/page")
    public Map<String, Object> page(@RequestBody T param) {
        return service.page(param);
    }
    @PostMapping("/e/list")
    public List<T> list(@RequestBody T param) {
        return service.list(param);
    }
    @PostMapping("/e/query")
    public List<T> query(@RequestBody T param) {
        return service.list(param);
    }
    @PostMapping("/e/save")
    public Object save(@RequestBody T param) {
        return service.save(param);
    }
    @GetMapping("/e/get/{primary}")
    public T get(@PathVariable("primary") Object primary) {
        return service.get(primary);
    }
    @PostMapping("/e/get")
    public T get(@RequestBody T param) {
        return service.get(param);
    }
    @PostMapping("/e/remove")
    public Object remove(@RequestBody T t) {
        return service.delete(t);
    }
    @GetMapping("/e/remove/{primary}")
    public Object remove(@PathVariable("primary") Object primary) {
        return service.delete(primary);
    }

    @RequestMapping("/table")
    public QTableModel getTable() {
        return this.service.getTable();
    }
    @RequestMapping("/primaryField")
    public QFieldModel getTablePrimaryField() {
        return this.service.getTablePrimaryField();
    }
    @PostMapping("saveField")
    public int saveField(@RequestBody QFieldModel field) {
        return this.service.saveField(field);
    }
    @PostMapping("/deleteField")
    public int deleteField(@RequestBody QFieldModel field) {
        return this.service.deleteField(field);
    }
    @GetMapping("/title")
    public JSONArray titles() {
        return this.service.titles();
    }
    @GetMapping("/insertForm")
    public JSONObject insertForm() {
        return this.service.insertForm();
    }
    @GetMapping("/updateForm")
    public JSONObject updateForm() {
        return this.service.updateForm();
    }
    @GetMapping("/conditionForm")
    public JSONObject conditionForm() {
        return this.service.selectForm();
    }
    @PostMapping("/page")
    public JSONObject page(@RequestBody JSONObject param) {
        return this.service.page(param);
    }
    @PostMapping("/list")
    public JSONArray list(@RequestBody JSONObject param) {
        return this.service.list(param);
    }
    @PostMapping("/query")
    public JSONArray query(@RequestBody JSONObject param) {
        return this.service.query(param);
    }
    @PostMapping("/save")
    public Object save(@RequestBody JSONObject param) {
        return this.service.save(param);
    }
    @PostMapping("/detail")
    public JSONObject detail(@RequestBody JSONObject param) {
        return this.service.detail(param);
    }
    @GetMapping("/detail/{primary}")
    public JSONObject detailByPrimary(@PathVariable("primary") Object primary) {
        return this.service.detail(primary);
    }
    @PostMapping("/delete")
    public Object delete(@RequestBody JSONObject param) {
        return this.service.delete(param);
    }
    @GetMapping("/delete/{primary}")
    public Object deleteByPrimaryKey(@PathVariable("primary") Object primary) {
        return this.service.delete(primary);
    }
    @GetMapping("/excel/template/xls")
    public void excelTemplateXls(HttpServletResponse response) throws IOException {
        this.service.excelTemplateXls(response);
    }
    @GetMapping("/excel/template/xlsx")
    public void excelTemplateXlsx(HttpServletResponse response) throws IOException {
        this.service.excelTemplateXlsx(response);
    }
    @PostMapping("/excel/import/xls")
    public int importExcelXls(MultipartFile file) throws IOException {
        return this.service.importExcelXls(file);
    }
    @PostMapping("/excel/import/xlsx")
    public int importExcelXlsx(MultipartFile file) throws IOException {
        return this.service.importExcelXlsx(file);
    }
    @PostMapping("/excel/export/xls")
    public void exportExcelXls(@RequestBody JSONObject param, HttpServletResponse response) throws IOException {
        this.service.exportExcelXls(param, response);
    }
    @PostMapping("/excel/export/xlsx")
    public void exportExcelXlsx(@RequestBody JSONObject param, HttpServletResponse response) throws IOException {
        this.service.exportExcelXlsx(param, response);
    }
}
